package com.ruoyi.manage.organization.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.CodeConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.ListUtil;
import com.ruoyi.common.utils.ObjUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.NumsUtils;
import com.ruoyi.manage.enterprise.service.IEcEnterpriseInformationService;
import com.ruoyi.manage.organization.domain.*;
import com.ruoyi.manage.organization.domain.vo.OcBusinessFinancingOrderVo;
import com.ruoyi.manage.organization.mapper.OcBusinessFinancingOrderMapper;
import com.ruoyi.manage.organization.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * 企业融资订单Service业务层处理
 *
 * @author ruoyi
 * @date 2025-08-19
 */
@Service
public class OcBusinessFinancingOrderServiceImpl extends ServiceImpl<OcBusinessFinancingOrderMapper, OcBusinessFinancingOrder> implements IOcBusinessFinancingOrderService {

    @Autowired
    private IOcOrderCreditService iOcOrderCreditService;

    @Autowired
    private IOcOrderLoanService iOcOrderLoanService;

    @Autowired
    private IOcFinancingMatchService iOcFinancingMatchService;

    @Autowired
    private IBizOrganizationService iBizOrganizationService;

    @Autowired
    private IEcEnterpriseInformationService iEcEnterpriseInformationService;


    @Override
    public OcBusinessFinancingOrder queryGetById(Long id) {
        OcBusinessFinancingOrder ocBusinessFinancingOrder = this.getById(id);
        return ocBusinessFinancingOrder;
    }

    /**
     * 新增企业融资订单
     *
     * @param ocBusinessFinancingOrder 企业融资订单
     * @return 结果
     */
    @Override
    @Transactional
    public boolean addOcBusinessFinancingOrder(OcBusinessFinancingOrder ocBusinessFinancingOrder) {

//       设置机构信息
        BizOrganization organization = iBizOrganizationService.lambdaQuery().eq(BizOrganization::getOrgPath, ocBusinessFinancingOrder.getOrgPath()).one();
        if (ObjUtil.isNotNull(organization)) {
            ocBusinessFinancingOrder.setOrganizationId(organization.getId());
            ocBusinessFinancingOrder.setOrganizationName(organization.getOrgName());
            ocBusinessFinancingOrder.setOrganizationType(organization.getOrgType());
        }

        ocBusinessFinancingOrder.setOrderNo(NumsUtils.generateCode("J"));
        SysUser user = SecurityUtils.getLoginUser().getUser();
        ocBusinessFinancingOrder.setManagerName(user.getNickName());
        ocBusinessFinancingOrder.setManagerId(user.getUserId());
        ocBusinessFinancingOrder.setManagerPhoto(user.getPhonenumber());
//        ocBusinessFinancingOrder.getOrderType(Long.valueOf());
        boolean save = this.save(ocBusinessFinancingOrder);

        iOcFinancingMatchService.lambdaUpdate().eq(OcFinancingMatch::getMachId, ocBusinessFinancingOrder.getMachId())
                .set(OcFinancingMatch::getState, "1").update();
        return save;
    }

    @Override
    public boolean grapOcBusinessFinancingOrder(OcBusinessFinancingOrder ocBusinessFinancingOrder) {
        //        修改抢单状态
        iOcFinancingMatchService.update(new UpdateWrapper<OcFinancingMatch>().set("state", "1").eq("mach_id", ocBusinessFinancingOrder.getMachId()));
//        查询机构
        BizOrganization organization = iBizOrganizationService.getOne(new QueryWrapper<BizOrganization>().eq("org_path", ocBusinessFinancingOrder.getOrgPath()));

        ocBusinessFinancingOrder.setOrderNo(NumsUtils.generateCode(CodeConstants.APPLY_ORDER));
        SysUser user = SecurityUtils.getLoginUser().getUser();
        ocBusinessFinancingOrder.setOrganizationId(organization.getId());
        ocBusinessFinancingOrder.setOrganizationName(organization.getOrgName());
        ocBusinessFinancingOrder.setOrganizationType(organization.getOrgType());
        ocBusinessFinancingOrder.setManagerId(user.getUserId());
        ocBusinessFinancingOrder.setManagerName(user.getNickName());
        ocBusinessFinancingOrder.setManagerPhoto(user.getPhonenumber());
        boolean save = this.save(ocBusinessFinancingOrder);
        if (!save) {
            throw new CustomException("保存失败");
        }
        return save;
    }

    @Override
    public boolean addCredit(OcBusinessFinancingOrderVo ocBusinessFinancingOrder) {

        //添加授信记录
        OcOrderCredit ocOrderCredit = ocBusinessFinancingOrder.getOcOrderCredit();
        ocOrderCredit.setOrderNo(ocBusinessFinancingOrder.getOrderNo());
        ocOrderCredit.setMachNo(ocBusinessFinancingOrder.getMachId());
        ocOrderCredit.setOrganizationName(ocBusinessFinancingOrder.getOrganizationName());
        ocOrderCredit.setOrgPath(ocBusinessFinancingOrder.getOrgPath());
        iOcOrderCreditService.save(ocOrderCredit);

        ocBusinessFinancingOrder.setCreditAmount(ocOrderCredit.getCreditAmount());
        ocBusinessFinancingOrder.setCreateTime(ocOrderCredit.getCreditTime());

//        更新企业进度
//        OcFinancingMatch financingMatch = iOcFinancingMatchService.lambdaQuery().eq(OcFinancingMatch::getMachId, ocBusinessFinancingOrder.getMachId()).one();
//        iEcEnterpriseInformationService.lambdaUpdate().eq(EcEnterpriseInformation::getId,financingMatch.getEnterpriseId()).set(EcEnterpriseInformation::getEnterpriseName,"");
        return this.updateById(ocBusinessFinancingOrder);
    }

    @Override
    public boolean addLoan(OcBusinessFinancingOrderVo ocBusinessFinancingOrder) {
        List<OcOrderLoan> ocOrderLoanList = ocBusinessFinancingOrder.getOcOrderLoanList();
        //添加用信记录
        for (OcOrderLoan ocOrderLoan : ocOrderLoanList) {
            ocOrderLoan.setOrderNo(ocBusinessFinancingOrder.getOrderNo());
            ocOrderLoan.setMachNo(ocBusinessFinancingOrder.getMachId());
            iOcOrderLoanService.save(ocOrderLoan);
            ocBusinessFinancingOrder.setLoanAmount(ocOrderLoan.getLoanAmount());
            ocBusinessFinancingOrder.setLoanTime(ocOrderLoan.getLoanTime());
        }

        return this.updateById(ocBusinessFinancingOrder);
    }

    @Override
    public List<OcBusinessFinancingOrder> selectFinancingOrderMatch(QueryWrapper ew) {
        return baseMapper.selectFinancingOrderMatch(ew);
    }

    /**
     * 修改企业融资订单
     *
     * @param ocBusinessFinancingOrder 企业融资订单
     * @return 结果
     */
    @Override
    public boolean updateOcBusinessFinancingOrder(OcBusinessFinancingOrder ocBusinessFinancingOrder) {
        return this.updateById(ocBusinessFinancingOrder);
    }

    /**
     * 批量删除企业融资订单
     *
     * @param ids 需要删除的企业融资订单主键
     * @return 结果
     */
    @Override
    public boolean deleteOcBusinessFinancingOrderByIds(Long[] ids) {
        return this.removeByIds(ListUtil.toList(ids));
    }

}
